<template>
    <section>

        <el-row>
            <!--工具条-->
            <el-col :span="23" class="toolbar toolMarginTop" id="serachId">
                <el-form :inline="true" :model="filters">
                    <el-button @click="supplierAdd" type="primary" size="small" style="float:left;">新增开发商</el-button>
                    <el-form-item style="float:right">
                        <el-button type="primary" v-on:click="getList" size="small">查询</el-button>
                    </el-form-item>
                    <el-form-item style="float:right">
                        <el-input v-model="filters.supplierName" placeholder="请输入开发商名称、联系人" size="small" @keyup.enter.native="searchEnterFun"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>

        <el-col :span="23" class="toolbar titleLineSymbol">
            <!--列表-->
            <el-table :data="tableData" align='center' highlight-current-row v-loading="listLoading"
                      style="width: 100%;">
                <el-table-column type="index" label="序号" min-width="60">
                </el-table-column>
                <el-table-column prop="supplierName" label="开发商名称" width="350">
                </el-table-column>
                <!-- <el-table-column prop="companyCode" label="开发商编码">
                </el-table-column>
                <el-table-column prop="sort" label="排序">
                </el-table-column> -->
                <el-table-column prop="lxr" label="联系人">
                </el-table-column>
                <el-table-column prop="lxdh" label="联系人电话">
                </el-table-column>
                <el-table-column prop="createUserName" label="创建人">
                </el-table-column>
                <el-table-column prop="createDate" label="创建时间" :formatter="formatCreateDate">
                </el-table-column>
                <!-- <el-table-column prop="updateDate" label="更新时间" :formatter="formatUpdateDate">
                </el-table-column> -->
                <el-table-column label="操作" min-width="150" align='center'>
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="marketDetails(scope.$index, scope.row)">详情</el-button>
                        <el-button type="text" size="mini" @click="marketEdit(scope.$index, scope.row)">修改</el-button>
                        <el-button type="text" size="mini" @click="SupplierDelete(scope.$index, scope.row)">删除
                        </el-button>
                        <el-button type="text" size="mini" @click="getSupplieruserList(scope.$index, scope.row)"
                                   v-if="false">分配
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-col :span="24" class="toolbar marginTop10px">
                <el-pagination layout="total, prev, pager, next, jumper" @current-change="handleCurrentPageChange"
                               :current-page="pageNum_main" :page-size="pageSize_main" :total="total_main">
                </el-pagination>
            </el-col>
        </el-col>
        </el-row>


        <!--编辑界面-->
        <!-- <el-dialog title="开发商编辑" :visible.sync="editFormVisible" :modal-append-to-body="false"> -->
        <Modal v-model="editFormVisible" title="修改开发商" width='50%'>
            <el-card>
                <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
                    <el-row>
                        <el-col :span="22">
                            <el-form-item label="开发商名称" prop="marketName">
                                <el-input v-model="editForm.supplierName" auto-complete="off" placeholder="不超过50个汉字"
                                          maxlength=50></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="12">
                            <el-form-item label="开发商编码">
                                <el-input v-model="editForm.companyCode"></el-input>
                            </el-form-item>
                        </el-col> -->
                    </el-row>
                    <el-row>
                        <el-col :span="11">
                            <el-form-item
                                    v-for="(xgform, index) in editForm.xgform"
                                    :label="'联系人' + (index+1)"
                                    :key="xgform.key"
                                    :prop="'xgform.' + index + '.lable'"
                                    :rules="{
								required: true, message: '联系人不能为空', trigger: 'blur'
							}">
                                <el-input v-model="xgform.lable"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="13">
                            <el-form-item
                                    v-for="(xgform, index) in editForm.xgform"
                                    :label="'联系人电话' + (index+1)"
                                    :key="xgform.key"
                                    :prop="'xgform.' + index + '.value'"
                                    :rules="{
										required: true, message: '联系人不能为空', trigger: 'blur'
									}"
                            >
                                <el-col :span="19">
                                    <el-input v-model="xgform.value"></el-input>
                                </el-col>
                                <el-col id="xgde" :span="1" style="margin-left:10px" v-if="index != 0">
                                    <el-button @click.prevent="removeEditForm(xgform)">删除</el-button>
                                </el-col>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="4">
                            <el-form-item>
                                <el-button @click.native="xiugaiForm">新增联系人</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col v-if="false">
                            <el-form-item label="排序">
                                <el-input v-model="editForm.sort"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-card>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">保存</el-button>
                <el-button @click.native="editFormVisible = false">取消</el-button>
            </div>
        </Modal>
        <!-- </el-dialog> -->

        <!--新增界面-->
        <!-- <el-dialog title="开发商注册" :visible.sync="addFormVisible" :modal-append-to-body="false"> -->
        <Modal v-model="addFormVisible" title="新增开发商" width='50%'>
            <el-card>
                <el-form :model="form" label-width="120px" :rules="addFormRules" ref="form">
                    <el-row>
                        <el-col :span="22">
                            <el-form-item label="开发商名称" prop="marketName"
                            	 :rules="{
								required: true, message: '开发商名称不能为空', trigger: 'blur'
							}">
                                <el-input v-model="form.supplierName" auto-complete="off" placeholder="不超过50个汉字"
                                          maxlength=50></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="11">
                            <el-form-item label="开发商编码">
                                <el-input v-model="form.companyCode"></el-input>
                            </el-form-item>
                        </el-col> -->
                    </el-row>
                    <el-row>
                        <el-col :span="11">
                            <el-form-item
                                    v-for="(addForm, index) in form.addForm"
                                    :label="'联系人' + (index+1)"
                                    :key="addForm.key"
                                    :prop="'addForm.' + index + '.lable'"
                                    :rules="{
								required: true, message: '联系人不能为空', trigger: 'blur'
							}">
                                <el-input v-model="addForm.lable"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="13">
                            <el-form-item
                                    v-for="(addForm, index) in form.addForm"
                                    :label="'联系人电话'+(index+1)"
                                    :key="addForm.key"
                                    :prop="'addForm.' + index + '.value'"
                                    :rules="{
								required: true, message: '联系人电话不能为空', trigger: 'blur'
							}">
                                <el-col :span="19">
                                    <el-input v-model="addForm.value"></el-input>
                                </el-col>
                                <el-col id="xgde" :span="1" style="margin-left:10px" v-if="index != 0">
                                    <el-button @click.prevent="removeAddForm(addForm)">删除</el-button>
                                </el-col>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="11">
                            <el-form-item>
                                <el-button @click.native="addForm">新增联系人</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col v-if="false">
                            <el-form-item label="排序">
                                <el-input v-model="form.sort"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-card>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click.native="addSubmit('form')" :loading="addLoading">保存</el-button>
                <el-button @click.native="addFormVisible = false">取消</el-button>
            </div>
        </Modal>
        <!-- </el-dialog> -->

        <!-- 分配	-->

        <!-- 分配页面 -->
        <Modal v-model="roleLoading" title="分配" :before-close="handleClose" width='50%'>
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="分配用户" name="first">
                    <!--列表-->
                    <el-col :span="24">
                        <el-button @click="userAdd">新增</el-button>
                    </el-col>
                    <el-table :data="supplierUserDate" style="width: 100%;">
                        <el-table-column type="index" label="序号" width="60">
                        </el-table-column>
                        <el-table-column prop="name" label="姓名" width="200">
                        </el-table-column>
                        <el-table-column prop="loginId" label="登入账号" width="200">
                        </el-table-column>
                        <el-table-column prop="createDate" label="创建时间" width="200" :formatter="formatCreateDate">
                        </el-table-column>
                        <el-table-column label="操作" min-width="200">
                            <template slot-scope="scope">
                                <el-button type="danger" size="small"
                                           @click="supplierAllocationDeleteByUser(scope.$index, scope.row)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-col :span="24" class="toolbar">
                        <el-pagination layout="total, prev, pager, next, jumper"
                                       @current-change="handleCurrentPageChange_user"
                                       :current-page="pageNum_user" :page-size="pageSize_user" :total="total_user">
                        </el-pagination>
                    </el-col>
                </el-tab-pane>
                <el-tab-pane label="分配市场" name="second">
                    <!--列表-->
                    <el-col :span="24">
                        <el-button @click="marketAdd">新增</el-button>
                    </el-col>
                    <el-table :data="supplierMarketDate" style="width: 100%;">
                        <el-table-column type="index" label="序号" width="60">
                        </el-table-column>
                        <el-table-column prop="marketName" label="市场名称" width="200">
                        </el-table-column>
                        <el-table-column prop="adminArea" label="行政区域" width="220" :formatter="formatAdminArea">
                        </el-table-column>
                        <el-table-column prop="createDate" label="修改时间" width="200" :formatter="formatCreateDate">
                        </el-table-column>
                        <el-table-column label="操作" min-width="200">
                            <template slot-scope="scope">
                                <el-button type="danger" size="small"
                                           @click="supplierAllocationDeleteByMarket(scope.$index, scope.row)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-col :span="24" class="toolbar">
                        <el-pagination layout="total, prev, pager, next, jumper"
                                       @current-change="handleCurrentPageChange_market"
                                       :current-page="pageNum_market" :page-size="pageSize_market"
                                       :total="total_market">
                        </el-pagination>
                    </el-col>
                </el-tab-pane>
                <el-tab-pane label="关联开发商" name="third">
                    <!--列表-->
                    <el-col :span="24">
                        <el-button @click="_supplierAdd">新增</el-button>
                    </el-col>
                    <el-table :data="supplierSupplierDate" style="width: 100%;">
                        <el-table-column type="index" label="序号" width="60">
                        </el-table-column>
                        <el-table-column prop="supplierName" label="开发商名称" width="200">
                        </el-table-column>
                        <el-table-column prop="companyCode" label="企业编码" width="200">
                        </el-table-column>
                        <el-table-column prop="createDate" label="修改时间" width="200" :formatter="formatCreateDate">
                        </el-table-column>
                        <el-table-column label="操作" min-width="200">
                            <template slot-scope="scope">
                                <el-button type="danger" size="small"
                                           @click="supplierAllocationDeleteBySupperlier(scope.$index, scope.row)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-col :span="24" class="toolbar">
                        <el-pagination layout="total, prev, pager, next, jumper"
                                       @current-change="handleCurrentPageChange_supplier"
                                       :current-page="pageNum_supplier" :page-size="pageSize_supplier"
                                       :total="total_supplier">
                        </el-pagination>
                    </el-col>
                </el-tab-pane>
                <!-- <el-tab-pane label="关联应用" name="fourth">关联应用</el-tab-pane> -->
            </el-tabs>
            <span slot="footer" class="dialog-footer">
			<el-button @click="roleLoading = false">取 消</el-button>
			<el-button type="primary" @click="roleLoading = false">确 定</el-button>
		</span>
        </Modal>

        <!--分配用户角色新增界面-->
        <Modal v-model="addUserLoading" title="分配用户新增" width='50%'>
            <!--列表-->
            <el-table :data="userDate" @selection-change="addSelsChange" style="width: 100%;">
                <el-table-column type="selection" width="55" :selectable="selectable">
                </el-table-column>
                <el-table-column type="index" label="序号" width="60">
                </el-table-column>
                <el-table-column prop="name" label="姓名" width="200">
                </el-table-column>
                <el-table-column prop="loginId" label="登入账号" width="200">
                </el-table-column>
                <el-table-column prop="createDate" label="修改时间" width="200" :formatter="formatCreateDate">
                </el-table-column>
            </el-table>
            <el-col :span="24" class="toolbar">
                <el-pagination layout="total, prev, pager, next, jumper"
                               @current-change="handleCurrentPageChange_user_user"
                               :current-page="pageNum_user_user" :page-size="pageSize_user_user"
                               :total="total_user_user">
                </el-pagination>
            </el-col>
            <span slot="footer" class="dialog-footer">
				<el-button @click="addUserLoading = false">取 消</el-button>
				<el-button type="primary" @click="addUserSubmit" :disabled="this.addSels.length===0">确 定</el-button>
			</span>
        </Modal>

        <!--分配市场新增界面-->

        <Modal v-model="addMarketLoading" title="分配市场新增" width='50%'>
            <!--列表-->
            <el-table :data="marketDate" @selection-change="addSelsChange" style="width: 100%;">
                <el-table-column type="selection" width="55" :selectable="selectable">
                </el-table-column>
                <el-table-column type="index" label="序号" width="60">
                </el-table-column>
                <el-table-column prop="marketName" label="市场名称" width="200">
                </el-table-column>
                <el-table-column prop="adminArea" label="行政区域" width="220" :formatter="formatAdminArea">
                </el-table-column>
                <el-table-column prop="createDate" label="修改时间" width="200" :formatter="formatCreateDate">
                </el-table-column>
            </el-table>
            <el-col :span="24" class="toolbar">
                <el-pagination layout="total, prev, pager, next, jumper"
                               @current-change="handleCurrentPageChange_market_market"
                               :current-page="pageNum_market_market" :page-size="pageSize_market_market"
                               :total="total_market_market">
                </el-pagination>
            </el-col>
            <span slot="footer" class="dialog-footer">
				<el-button @click="addMarketLoading = false">取 消</el-button>
				<el-button type="primary" @click="addMarketSubmit" :disabled="this.addSels.length===0">确 定</el-button>
			</span>
        </Modal>

        <!--分配 关联开发商 新增界面-->
        <Modal v-model="addSupplierLoading" title="关联开发商新增" width='50%'>
            <!--列表-->
            <el-table :data="supplierDate" @selection-change="addSelsChange" style="width: 100%;">
                <el-table-column type="selection" width="55" :selectable="selectable">
                </el-table-column>
                <el-table-column type="index" label="序号" width="60">
                </el-table-column>
                <el-table-column prop="supplierName" label="开发商名称" width="200">
                </el-table-column>
                <el-table-column prop="companyCode" label="企业编码" width="200">
                </el-table-column>
                <el-table-column prop="createDate" label="修改时间" width="200" :formatter="formatCreateDate">
                </el-table-column>
            </el-table>
            <el-col :span="24" class="toolbar">
                <el-pagination layout="total, prev, pager, next, jumper"
                               @current-change="handleCurrentPageChange_supplier_supplier"
                               :current-page="pageNum_supplier_supplier" :page-size="pageSize_supplier_supplier"
                               :total="total_supplier_supplier">
                </el-pagination>
            </el-col>
            <span slot="footer" class="dialog-footer">
				<el-button @click="addSupplierLoading = false">取 消</el-button>
				<el-button type="primary" @click="addSupplierSubmit" :disabled="this.addSels.length===0">确 定</el-button>
			</span>
        </Modal>

    </section>
</template>

<script>

    import util from '@/views/api/utils.js';
    //import NProgress from 'nprogress'
    import {
        addSupplier,
        deleteSupplier,
        deletesupplierAllocationByMarket,
        deletesupplierAllocationBySupperlier,
        deletesupplierAllocationByUser,
        distribution,
        editSupplier,
        getMarketListBySupplier,
        getSupplierList,
        getSupplierListBySupplier,
        getSupplierMarketList,
        getSupplierSupplierList,
        getSupplierUserList,
        getUserListBySupplier
    } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    supplierName: ''
                },
                tableData: [],
                // 主页面分页
                total_main: 0,
                pageNum_main: 1,
                pageSize_main: 10,
                sels: [],//列表选中列
                listLoading: false,

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    supplierName: [
                        {required: true, message: '请输入开发商名称', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    xgform: [{
                        lable: '',
                        value: ''
                    }],
                    supplierName: '',
                    // companyCode: '',
                    sort: 0,
                    lxr: '',
                    lxdh: ''
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    supplierName: [
                        {required: true, message: '请输入开发商名称、联系人', trigger: 'blur'}
                    ]
                },
                //新增界面数据
                form: {
                    addForm: [{
                        lable: '',
                        value: ''
                    }],
                    supplierName: '',
                    companyCode: '',
                    sort: 0,
                    lxr: '',
                    lxdh: ''
                },

                //分配 begin
                roleLoading: false,
                activeName: 'first',
                // 分配 分配用户
                // 列表数据
                supplierUserDate: [],
                // 分页
                total_user: 0,
                pageNum_user: 1,
                pageSize_user: 2,
                supplierCode: '',
                //分配用户新增是否显示
                addUserLoading: false,
                //分配用户新增界面数据
                userDate: [],
                addSels: [],//列表选中列
                //用户列表分页
                total_user_user: 0,
                pageNum_user_user: 1,
                pageSize_user_user: 2,
                // 分配 分配市场
                // 列表数据
                supplierMarketDate: [],
                // 分页
                total_market: 0,
                pageNum_market: 1,
                pageSize_market: 2,
                supplierCode: '',
                //分配市场新增是否显示
                addMarketLoading: false,
                //分配用户新增界面数据
                marketDate: [],
                addMarketSels: [],//列表选中列
                //分配市场分页
                total_market_market: 0,
                pageNum_market_market: 1,
                pageSize_market_market: 2,
                // 分配 关联开发商
                // 列表数据
                supplierSupplierDate: [],
                // 分页
                total_supplier: 0,
                pageNum_supplier: 1,
                pageSize_supplier: 2,
                supplierCode: '',
                //分配市场新增是否显示
                addSupplierLoading: false,
                //分配用户新增界面数据
                supplierDate: [],
                addSupplierSels: [],//列表选中列
                //分配 关联开发商 分页
                total_supplier_supplier: 0,
                pageNum_supplier_supplier: 1,
                pageSize_supplier_supplier: 2,

            }
        },
        methods: {
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            handleClick(tab, event) {
                //console.log(tab, event);
                if (tab.name == 'second') {
                    this.getSuppliermarketList();
                } else if (tab.name == 'third') {
                    this.getSuppliersupplierList();
                }
            },
            //时间格式化
            formatCreateDate: function (row, column) {
                return row.createDate = (!row.createDate || row.createDate == '') ? '' : util.formatDate.format(new Date(row.createDate), 'yyyy-MM-dd hh:mm');
            },
            formatUpdateDate: function (row, column) {
                return row.updateDate = (!row.updateDate || row.updateDate == '') ? '' : util.formatDate.format(new Date(row.updateDate), 'yyyy-MM-dd hh:mm:ss');
            },
            formatAdminArea: function (row, column) {
                let xzqh = JSON.parse(row.adminArea);
                let xzqhName = "";
                try {
                    xzqh.forEach(obj => {
                        xzqhName += obj.name;
                    });
                } catch (e) {
                    console.log(e)
                }
                return xzqhName;
            },
            handleCurrentPageChange: function (pageNum) {// 翻页动作
                this.pageNum_main = pageNum;
                this.getList();// 翻页后刷新页面数据
            },
            handleCurrentPageChange_user: function (pageNum) {// 翻页动作
                this.pageNum_user = pageNum;
                let params = {
                    supplierCode: this.supplierCode
                };
                this.getSupplieruserList(1, params);// 翻页后刷新页面数据
            },
            handleCurrentPageChange_user_user: function (pageNum) {// 翻页动作
                this.pageNum_user_user = pageNum;
                this.userAdd();// 翻页后刷新页面数据
            },
            handleCurrentPageChange_market: function (pageNum) {// 翻页动作
                this.pageNum_market = pageNum;
                let params = {
                    supplierCode: this.supplierCode
                };
                this.getSuppliermarketList();// 翻页后刷新页面数据
            },
            handleCurrentPageChange_market_market: function (pageNum) {// 翻页动作
                this.pageNum_market_market = pageNum;
                this.marketAdd();// 翻页后刷新页面数据
            },
            handleCurrentPageChange_supplier: function (pageNum) {
                this.pageNum_supplier = pageNum;
                let params = {
                    supplierCode: this.supplierCode
                };
                this.getSuppliersupplierList();// 翻页后刷新页面数据
            },
            handleCurrentPageChange_supplier_supplier: function (pageNum) {
                this.pageNum_supplier_supplier = pageNum;
                this._supplierAdd();// 翻页后刷新页面数据
            },
            //实现回车键搜索功能
			searchEnterFun:function(e){
				this.getList();
            },
            //获取列表
            getList() {
                let params = {
                    pageNum: this.pageNum_main,
                    pageSize: this.pageSize_main,
                    supplierName: this.filters.supplierName
                };
                this.listLoading = true;
                //NProgress.start();
                getSupplierList(params).then(function (result) {
                    this.tableData = result.data.list;
                    this.pageNum_main = result.data.pageNum;
                    this.pageSize_main = result.data.pageSize;
                    this.total_main = result.data.total;
                    this.listLoading = false;
                }.bind(this)).catch(function (error) {
                    this.listLoading = false;
                    console.log(error);
                }.bind(this));
            },
            //删除
            SupplierDelete: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let params = {supplierCode: row.supplierCode};
                    deleteSupplier(params).then((res) => {
                        if (res.code == 'B100001') {
                            this.$message({
                                message: "开发商正在使用，不允许删除！",
                                type: 'info'
                            });
                        } else {
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }
                        this.listLoading = false;
                        this.getList();
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //显示编辑界面
            marketEdit: function (index, row) {
                this.editFormVisible = true;
                let arr = [];
                if (row.lxr != null && row.lxdh != null) {
                    let lxrArr = row.lxr.split(";");
                    let lxdhArr = row.lxdh.split(";");
                    for (let i = 0; i < lxrArr.length; i++) {
                        let obj = {
                            lable: lxrArr[i],
                            value: lxdhArr[i]
                        }
                        arr.push(obj);
                    }

                }
                this.editForm = Object.assign({}, row);
                this.editForm.xgform = arr;
                console.log(">>>>>>>>>>>>>>>>>>>.")
                console.log(index)
                console.log(row)
                console.log(this.editForm)
            },
            //显示新增界面
            supplierAdd: function () {
                this.addFormVisible = true;
                this.form = {
                    addForm: [{
                        lable: '',
                        value: ''
                    }],
                    supplierName: '',
                    companyCode: '',
                    sort: 0,
                    lxr: '',
                    lxdh: ''
                };
            },
            addForm() {
                if (this.form.addForm.length > 8) {
                    this.$message({
                        message: '最多添加9个联系人！',
                        type: 'info'
                    });
                    return
                }
                this.form.addForm.push({
                    lable: '',
                    value: '',
                    key: Date.now()
                });
            },
            xiugaiForm() {
                if (this.editForm.xgform.length > 8) {
                    this.$message({
                        message: '最多添加9个联系人！',
                        type: 'info'
                    });
                    return
                }
                this.editFormVisible = false;
                this.editForm.xgform.push({
                    lable: '',
                    value: '',
                    key: Date.now()
                });
                this.editFormVisible = true;
            },
            removeAddForm(item) {
                let index = this.form.addForm.indexOf(item);
                if (index === 0) {
                    this.$message({
                        message: '最少需要有一个联系人！',
                        type: 'info'
                    });
                    return
                }
                if (index !== -1) {
                    this.form.addForm.splice(index, 1)
                }
            },
            removeEditForm(item) {
                let index = this.editForm.xgform.indexOf(item)
                if (index === 0) {
                    this.$message({
                        message: '最少需要有一个联系人！',
                        type: 'info'
                    });
                    return
                }
                this.editFormVisible = false;
                if (index !== -1) {
                    this.editForm.xgform.splice(index, 1)
                }
                this.editFormVisible = true;
            },
            //编辑
            editSubmit: function () {
                let _this = this;
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let params = Object.assign({}, this.editForm);
                            //格式化提交的创建时间与更新时间
                            params.createDate = Date.parse(new Date(params.createDate));
                            if (params.updateDate != '') {
                                params.updateDate = Date.parse(new Date(params.updateDate));
                            }
                            if (util.isNotEmpty(params.lxr) && util.isNotEmpty(_this.editForm)) {
                                params.lxr = "";
                            }
                            if (util.isNotEmpty(params.lxdh) && util.isNotEmpty(_this.editForm)) {
                                params.lxdh = "";
                            }
                            _this.editForm.xgform.forEach(element => {
                                params.lxr += element.lable + ";";
                                params.lxdh += element.value + ";";
                            });
                            params.lxr = params.lxr.substring(0, params.lxr.length - 1);
                            params.lxdh = params.lxdh.substring(0, params.lxdh.length - 1);
                            editSupplier(params).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                // this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getList();
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let params = Object.assign({}, this.form);
                            this.form.addForm.forEach(element => {
                                params.lxr += element.lable + ";";
                                params.lxdh += element.value + ";";
                            });
                            params.lxr = params.lxr.substring(0, params.lxr.length - 1);
                            params.lxdh = params.lxdh.substring(0, params.lxdh.length - 1);
                            addSupplier(params).then((res) => {
                                if (res.code == 'B100001') {
                                    this.$message({
                                        message: "已存在该开发商",
                                        type: 'info'
                                    });
                                } else {
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                }
                                this.addLoading = false;
                                // this.$refs['form'].resetFields();
                                this.addFormVisible = false;
                                this.pageNum = 1;
                                this.pageSize = 2;
                                this.getList();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            selectable: function (row, index) {
                if (row.jurisdiction != null) {
                    return false;
                } else {
                    return true;
                }
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {ids: ids};
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            },
            getSupplieruserList(index, row) {
                let params = {
                    pageNum: this.pageNum_user,
                    pageSize: this.pageSize_user,
                    supplierCode: row.supplierCode
                };
                this.supplierCode = row.supplierCode;
                //NProgress.start();
                getSupplierUserList(params).then(function (result) {
                    this.supplierUserDate = result.data.list;
                    this.pageNum_user = result.data.pageNum;
                    this.pageSize_user = result.data.pageSize;
                    this.total_user = result.data.total;
                    this.roleLoading = true;
                    //每次进去显示第一个页签
                    this.activeName = 'first';
                }.bind(this)).catch(function (error) {
                    console.log(error);
                }.bind(this));
            },
            userAdd: function () {
                let params = {
                    pageNum: this.pageNum_user_user,
                    pageSize: this.pageSize_user_user,
                    supplierCode: this.supplierCode
                };
                //NProgress.start();
                getUserListBySupplier(params).then(function (result) {
                    this.userDate = result.data.list;
                    this.pageNum_user_user = result.data.pageNum;
                    this.pageSize_user_user = result.data.pageSize;
                    this.total_user_user = result.data.total;
                    this.addUserLoading = true;
                }.bind(this)).catch(function (error) {
                    console.log(error);
                }.bind(this));
            },
            addSelsChange: function (addSels) {
                this.addSels = addSels;
            },
            addUserSubmit: function () {
                var userIdList = this.addSels.map(item => item.userId).toString();
                this.$confirm('确认要分配选中的用户吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    //NProgress.start();
                    let params = {
                        supplierCode: this.supplierCode,
                        userIdList: userIdList
                    };
                    distribution(params).then((res) => {
                        //NProgress.done();
                        this.$message({
                            message: '分配成功',
                            type: 'success'
                        });
                        this.addUserLoading = false;
                        this.pageNum_user = 1;
                        let _params = {
                            supplierCode: this.supplierCode
                        };
                        this.addSels = [];
                        this.getSupplieruserList(1, _params);
                    });
                }).catch(() => {
                    this.addUserLoading = false;
                });
            },
            getSuppliermarketList() {
                let params = {
                    pageNum: this.pageNum_market,
                    pageSize: this.pageSize_market,
                    supplierCode: this.supplierCode
                };
                //NProgress.start();
                getSupplierMarketList(params).then(function (result) {
                    this.supplierMarketDate = result.data.list;
                    this.pageNum_market = result.data.pageNum;
                    this.pageSize_market = result.data.pageSize;
                    this.total_market = result.data.total;
                    this.roleLoading = true;
                }.bind(this)).catch(function (error) {
                    console.log(error);
                }.bind(this));
            },
            marketAdd: function () {
                let params = {
                    pageNum: this.pageNum_market_market,
                    pageSize: this.pageSize_market_market,
                    supplierCode: this.supplierCode
                };
                //NProgress.start();
                getMarketListBySupplier(params).then(function (result) {
                    this.marketDate = result.data.list;
                    this.pageNum_market_market = result.data.pageNum,
                        this.pageSize_market_market = result.data.pageSize,
                        this.total_market_market = result.data.total,
                        this.addMarketLoading = true;
                }.bind(this)).catch(function (error) {
                    console.log(error);
                }.bind(this));
            },
            addMarketSubmit: function () {
                var marketCodes = this.addSels.map(item => item.marketCode).toString();
                this.$confirm('确认要分配选中的用户吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    //NProgress.start();
                    let params = {
                        supplierCode: this.supplierCode,
                        marketCodes: marketCodes
                    };
                    distribution(params).then((res) => {
                        //NProgress.done();
                        this.$message({
                            message: '分配成功',
                            type: 'success'
                        });
                        this.addMarketLoading = false;
                        this.pageNum_market = 1;
                        this.addSels = [];
                        this.getSuppliermarketList();
                    });
                }).catch(() => {
                    this.addMarketLoading = false;
                });
            },
            getSuppliersupplierList() {
                let params = {
                    pageNum: this.pageNum_supplier,
                    pageSize: this.pageSize_supplier,
                    supplierCode: this.supplierCode
                };
                //NProgress.start();
                getSupplierSupplierList(params).then(function (result) {
                    this.supplierSupplierDate = result.data.list;
                    this.pageNum_supplier = result.data.pageNum;
                    this.pageSize_supplier = result.data.pageSize;
                    this.total_supplier = result.data.total;
                    this.supplierLoading = true;
                }.bind(this)).catch(function (error) {
                    console.log(error);
                }.bind(this));
            },
            _supplierAdd: function () {
                let params = {
                    pageNum: this.pageNum_supplier_supplier,
                    pageSize: this.pageSize_supplier_supplier,
                    supplierCode: this.supplierCode
                };
                //NProgress.start();
                getSupplierListBySupplier(params).then(function (result) {
                    this.supplierDate = result.data.list;
                    this.pageNum_supplier_supplier = result.data.pageNum;
                    this.pageSize_supplier_supplier = result.data.pageSize;
                    this.total_supplier_supplier = result.data.total;
                    this.addSupplierLoading = true;
                }.bind(this)).catch(function (error) {
                    console.log(error);
                }.bind(this));
            },
            addSupplierSubmit: function () {
                var supplierCodes = this.addSels.map(item => item.supplierCode).toString();
                this.$confirm('确认要分配选中的用户吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    //NProgress.start();
                    let params = {
                        supplierCode: this.supplierCode,
                        supplierCodes: supplierCodes
                    };
                    distribution(params).then((res) => {
                        //NProgress.done();
                        this.$message({
                            message: '分配成功',
                            type: 'success'
                        });
                        this.addSupplierLoading = false;
                        this.pageNum_supperlier = 1;
                        this.addSels = [];
                        this.getSuppliersupplierList();
                    });
                }).catch(() => {
                    this.addSupplierLoading = false;
                });
            },
            supplierAllocationDeleteByUser: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.roleLoading = true;
                    //NProgress.start();
                    let params = {
                        id: row.userId,
                        supplierCode: this.supplierCode
                    };
                    deletesupplierAllocationByUser(params).then((res) => {
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        let _params = {
                            supplierCode: this.supplierCode
                        };
                        this.getSupplieruserList(1, _params);
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            supplierAllocationDeleteByMarket: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.roleLoading = true;
                    //NProgress.start();
                    let params = {
                        marketCode: row.marketCode,
                        supplierCode: this.supplierCode
                    };
                    deletesupplierAllocationByMarket(params).then((res) => {
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getSuppliermarketList();
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            supplierAllocationDeleteBySupperlier: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.roleLoading = true;
                    //NProgress.start();
                    let params = {
                        linkSupplier: row.supplierCode,
                        supplierCode: this.supplierCode
                    };
                    deletesupplierAllocationBySupperlier(params).then((res) => {
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getSuppliersupplierList();
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
        },
        mounted() {
            this.getList();
        }
    }
</script>